#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

CFLAGS = -Wall -g
#WARN: doesn't compile/package if HAS_LIBRARY_* options not present (since we use configure now)
MAKE_FLAGS = messages=yes OPTFLAG=-O2 HAS_LIBRARY_ldap=yes HAS_LIBRARY_pq=yes HAS_LIBRARY_xml2=yes

GNUSTEP_SETUP=/usr/share/GNUstep/Makefiles/GNUstep.sh

include Version
SOPEVER=$(MAJOR_VERSION).$(MINOR_VERSION)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

controlfiles: controlfiles-stamp debian/control
controlfiles-stamp: debian/rules clean-controlfiles
	for f in debian/*_SOPEVER_*; do \
		newname=$$(echo $$f | sed s/_SOPEVER_/$(SOPEVER)/ ); \
		cp $$f $$newname; \
		echo $$newname >> controlfiles.tmp; \
	done

	mv controlfiles.tmp controlfiles-stamp

clean-controlfiles:
	if [ -f controlfiles-stamp ]; then \
		for f in $$(cat controlfiles-stamp); do \
			rm $$f; \
		done; \
		rm controlfiles-stamp; \
	fi
	if [ -f controlfiles.tmp ]; then \
		for f in $$(cat controlfiles-stamp); do \
			rm $$f; \
		done; \
		rm controlfiles.tmp; \
	fi

debian/control: debian/control.in debian/rules
	sed -e s/_SOPEVER_/$(SOPEVER)/g < debian/control.in > debian/control

config.make:
	dh_testdir
	CFLAGS="$(CFLAGS)" . $(GNUSTEP_SETUP); \
		./configure --with-gnustep --enable-debug --disable-strip

build: config.make build-stamp controlfiles
build-stamp: config.make
	dh_testdir

	-mv sope-core/NGStreams/config.guess sope-core/NGStreams/config.guess.upstream
	-mv sope-core/NGStreams/config.sub sope-core/NGStreams/config.sub.upstream
	ln -s /usr/share/misc/config.guess sope-core/NGStreams/config.guess
	ln -s /usr/share/misc/config.sub sope-core/NGStreams/config.sub

	CFLAGS="$(CFLAGS)" . $(GNUSTEP_SETUP); \
		$(MAKE) $(MAKE_FLAGS) all

#	cp -R sope-appserver/mod_ngobjweb sope-appserver/mod_ngobjweb-apache2

#	CFLAGS="$(CFLAGS)" . $(GNUSTEP_SETUP); \
#		$(MAKE) $(MAKE_FLAGS) APXS=/usr/bin/apxs -C sope-appserver/mod_ngobjweb all

#	CFLAGS="$(CFLAGS)" . $(GNUSTEP_SETUP); \
#		$(MAKE) $(MAKE_FLAGS) APXS="/usr/bin/apxs2" APXS_INCLUDE_DIRS="$(shell apxs2 -q EXTRA_INCLUDES)"\
#		-C sope-appserver/mod_ngobjweb-apache2 all

	touch build-stamp

clean: clean-controlfiles
	dh_testdir
	dh_testroot
	rm -f configure-stamp
	rm -f build-stamp
	rm -f config-NSStreams.log
	rm -rf sope-appserver/mod_ngobjweb-apache2
	rm -rf sope-core/NGStreams/x86_64

	if [ -L sope-core/NGStreams/config.guess ]; then \
		rm sope-core/NGStreams/config.guess; \
	fi
	if [ -L sope-core/NGStreams/config.sub ]; then \
		rm sope-core/NGStreams/config.sub; \
	fi
	if [ -f sope-core/NGStreams/config.guess.upstream ]; then \
		mv sope-core/NGStreams/config.guess.upstream sope-core/NGStreams/config.guess; \
	fi
	if [ -f sope-core/NGStreams/config.sub.upstream ]; then \
		mv sope-core/NGStreams/config.sub.upstream sope-core/NGStreams/config.sub; \
	fi

#	-. $(GNUSTEP_SETUP); $(MAKE) -C sope-appserver/mod_ngobjweb clean
#	-. $(GNUSTEP_SETUP); $(MAKE) -C sope-appserver/mod_ngobjweb distclean

	-. $(GNUSTEP_SETUP); $(MAKE) clean
	-. $(GNUSTEP_SETUP); $(MAKE) distclean

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

#	Add here commands to install the package into debian/tmp
	CFLAGS="$(CFLAGS)" . $(GNUSTEP_SETUP); \
		$(MAKE) $(MAKE_FLAGS) install \
		DESTDIR=$(CURDIR)/debian/tmp GNUSTEP_INSTALLATION_DOMAIN=SYSTEM
#		INSTALL_ROOT_DIR=$(CURDIR)/debian/tmp\
		FHS_INSTALL_ROOT=$(CURDIR)/debian/tmp/usr \
		FHS_LIB_DIR=$(CURDIR)/debian/tmp/usr/lib/ #WARN: <- trailing slash unbroke build/packages?!

#	CFLAGS="$(CFLGAS)" . $(GNUSTEP_SETUP); \
		$(MAKE) $(MAKE_FLAGS) -C sope-appserver/mod_ngobjweb install \
		GNUSTEP_INSTALLATION_DOMAIN=$(CURDIR)/debian/tmp/usr/lib/apache/1.3

#	CFLAGS="$(CFLGAS)" . $(GNUSTEP_SETUP); \
		$(MAKE) $(MAKE_FLAGS) -C sope-appserver/mod_ngobjweb-apache2 install \
		GNUSTEP_INSTALLATION_DOMAIN=$(CURDIR)/debian/tmp/usr/lib/apache2/modules

#	install -m 644 debian/500mod_ngobjweb.info $(CURDIR)/debian/libapache-mod-ngobjweb/usr/lib/apache/1.3
#	install -m 644 debian/ngobjweb.load $(CURDIR)/debian/libapache2-mod-ngobjweb/etc/apache2/mods-available

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_install --sourcedir=debian/tmp
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip --dbg-package=sope$(SOPEVER)-dbg
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps -L libsope-appserver$(SOPEVER) -L libsope-xml$(SOPEVER) -L libsope-mime$(SOPEVER) \
		-L libsope-ldap$(SOPEVER) -L libsope-gdl1-$(SOPEVER) \
		-l debian/tmp/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
